<?php

class MembersController extends Controller{
 
	 public function actionProfile(){
		$this->layout = '//layouts/usersMembersProfile';
		if (isset($_GET['id'])) {
			$model = Members::model()->findByPk($_GET['id']);
			$this->render('profile',array('model'=>$model));
		}
		
	 }

 	public function actionUploadImageBanner()
	{
			Yii::import("ext.EAjaxUpload.qqFileUploader");
	        $folder= 'images/Banner/originimage/';// folder for uploaded files
	        $allowedExtensions = array("jpg","jpeg","png", "gif", "JPG", "JPEG", "PNG", "GIF");//array("jpg","jpeg","gif","exe","mov" and etc...
	        $sizeLimit = 4 * 1024 * 1024;// maximum file size in bytes
	        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
	        $result = $uploader->handleUpload($folder);
	        $return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
	        $fileSize=filesize($folder.$result['filename']);//GETTING FILE SIZE
	        $fileName=$result['filename'];//GETTING FILE NAME
			$src = 'images/Banner/originimage/'.$fileName;
			$dest = 'images/Banner/resize/'.$fileName;
			list($width, $height, $type, $attr) = getimagesize($src);
			if ($width > 1040){
				Yii::import("ext.EPhpThumb.EPhpThumb");
				$resize=new EPhpThumb();
				$resize->init();
				$tile998 = 1040 / $width;
				$height998 = $height * $tile998;
				$resize->create($src)->resize(1040, $height998)->save($dest);
			}else{
				copy($src, $dest);
			}
			echo $return;
	}

	public function actionCoverPhotoBanner()
	{
		if (Yii::app()->request->isAjaxRequest)
		{
			if ($_POST['typeGet'] == 1)
			{
				$src_resize = 'images/Banner/resize/'.$_POST['namephoto'];
				$dest_resize = 'images/Banner/resize/'.$_POST['namephoto'];
				$src_origin = 'images/Banner/originimage/'.$_POST['namephoto'];
				$dest_origin = 'images/Banner/originimage/'.$_POST['namephoto'];
				$dest_173_150 = 'images/Banner/173_150/'.$_POST['namephoto'];
				copy($src_origin, $dest_origin);
				copy($src_resize, $dest_resize);
				// resize photo width = 173 and height = 150
				$this->resizePhoto($dest_origin, $dest_173_150);
			}else{
				switch ($_POST['typecover']) {
					case 'D':
						$src = 'images/Member/originimage/'.$_POST['namephoto'];
						$dest = 'images/Banner/originimage/'.$_POST['namephoto'];
						$dest_resize = 'images/Banner/resize/'.$_POST['namephoto'];
						$src_173_150 = 'images/Member/173_150/'.$_POST['namephoto'];
						$dest_173_150 = 'images/Banner/173_150/'.$_POST['namephoto'];
						copy($src_173_150, $dest_173_150);
						copy($src, $dest);
						// resize photo to size width = 998 and height = auto;
						$this->resizePhotoBanner($src, $dest_resize);
						break;
					case 'C':
						$src = 'images/Banner/cover/'.$_POST['namephoto'];
						if (file_exists($src))
						{
							unlink($src);
						}
						$dest_resize = 'images/Banner/resize/'.$_POST['namephoto'];
						break;

					case 'A':
						$src = 'images/Avatar/originimage/'.$_POST['namephoto'];
						$dest = 'images/Banner/originimage/'.$_POST['namephoto'];
						$dest_resize = 'images/Banner/resize/'.$_POST['namephoto'];
						$src_173_150 = 'images/Avatar/173_150/'.$_POST['namephoto'];
						$dest_173_150 = 'images/Banner/173_150/'.$_POST['namephoto'];
						copy($src_173_150, $dest_173_150);
						copy($src, $dest);
						// resize photo to size width = 998 and height = auto;
						$this->resizePhotoBanner($src, $dest_resize);
						break;

					case 'M':
						$src = 'images/Member/originimage/'.$_POST['namephoto'];
						$dest = 'images/Banner/originimage/'.$_POST['namephoto'];
						$dest_resize = 'images/Banner/resize/'.$_POST['namephoto'];
						$src_173_150 = 'images/Member/173_150/'.$_POST['namephoto'];
						$dest_173_150 = 'images/Banner/173_150/'.$_POST['namephoto'];
						copy($src_173_150, $dest_173_150);
						copy($src, $dest);
						// resize photo to size width = 998 and height = auto;
						$this->resizePhotoBanner($src, $dest_resize);
						break;
				}
			}
			list($width, $height, $type, $attr) = getimagesize($dest_resize);
			$path = 'images/Banner/cover/'.$_POST['namephoto'];
			$_POST['x'] = 0;
		    $_POST['h'] = 300;
		    $tile = $width / 1040;
		    $y = $_POST['y'] * $tile;
		    $h = $_POST['h'] * $tile;
		    $x = 0;
		    $wi = $width;
		    $he = $h;
			Yii::import("ext.EPhpThumb.EPhpThumb");
			$crop = new EPhpThumb();
			$crop->init();
			$crop->create($dest_resize)->crop($x, $y, $wi, $he)->save($path);
			$coverPast = MemberCovers::model()->findByAttributes(array('member_id'=>$_POST['memberid'], 'status'=>'V', 'type'=>'C'));
			if ( $_POST['typecover'] != 'C')
			{
				$model = new MemberCovers;
				$model->member_id = $_POST['memberid'];
				$model->image = $_POST['namephoto'];
				$model->originimage = $_POST['namephoto'];
				$model->status = 'V';
				$model->type = 'C';
				$model->posteddate = date('Y-m-d H:i:s');
				if ($model->save(false))
				{
					if ($coverPast !== null)
						MemberCovers::model()->updateByPk($coverPast->id, array('status'=>'H'));
				}
			}else
			{
				$model = MemberCovers::model()->findByAttributes(array('member_id'=>$_POST['memberid'], 'originimage'=>$_POST['namephoto'], 'type'=>'C'));
				$model->attributes = array('status'=>'V');
				if ($model->save(false))
				{
					if ($coverPast !== null && $model->id != $coverPast->id)
						MemberCovers::model()->updateByPk($coverPast->id, array('status'=>'H'));
				}
			}
			echo $model->image;
		}
	}

	// get all album member when choosing photo cover
	public function actionGetAlbumPhoto()
	{
			$thumbAlbum = array();
			$album = MemberAlbums::model()->findAllByAttributes(array('member_id'=>$_GET['memberid']));
			$thumb_album_default = MemberImages::model()->findByAttributes(array('album_id'=>0, 'member_id'=>$_GET['memberid']));
			$thumb_album_banner = MemberCovers::model()->findByAttributes(array('member_id'=>$_GET['memberid'], 'type'=>'C', 'status'=>'V'));
			$thumb_album_avatar = MemberCovers::model()->findByAttributes(array('member_id'=>$_GET['memberid'], 'type'=>'A', 'status'=>'V'));
			foreach($album as $data){
				$thumb = MemberImages::model()->findByAttributes(array('album_id'=>$data->id, 'member_id'=>$_GET['memberid']));
				if ($thumb !== null)
				{
					$thumbAlbum[$data->id] = $thumb->image;
				}
			}
			echo $this->renderPartial('getAlbumPhoto', array('thumbAlbum'=>$thumbAlbum, 'album'=>$album, 'thumb_album_default'=>$thumb_album_default, 'thumb_album_banner'=>$thumb_album_banner, 'thumb_album_avatar'=>$thumb_album_avatar), false, true);
	}

	// view list photo when chossing list album
	public function actionViewListPhoto()
	{
			switch ($_POST['type']) {
				case 'D':
					$model = MemberImages::model()->findAllByAttributes(array('album_id'=>0, 'member_id'=>$_POST['memberid']));
					break;
				
				case 'C':
					$model = MemberCovers::model()->findAllByAttributes(array('member_id'=>$_POST['memberid'], 'type'=>$_POST['type']));
					break;

				case 'A':
					$model = MemberCovers::model()->findAllByAttributes(array('member_id'=>$_POST['memberid'], 'type'=>$_POST['type']));
					break;

				case 'M':
					$model = MemberImages::model()->findAllByAttributes(array('album_id'=>$_POST['albumid'], 'member_id'=>$_POST['memberid']));
					break;
			}
			echo $this->renderPartial('viewListPhoto', array('model'=>$model, 'type'=>$_POST['type']), false, true);
	}

	public function actionViewListAlbumPhoto()
	{
 
			$thumbAlbum = array();
			$album = MemberAlbums::model()->findAllByAttributes(array('member_id'=>$_POST['memberid']));
			$thumb_album_default = MemberImages::model()->findByAttributes(array('album_id'=>0, 'member_id'=>$_POST['memberid']));
			$thumb_album_banner = MemberCovers::model()->findByAttributes(array('member_id'=>$_POST['memberid'], 'type'=>'C', 'status'=>'V'));
			$thumb_album_avatar = MemberCovers::model()->findByAttributes(array('member_id'=>$_POST['memberid'], 'type'=>'A', 'status'=>'V'));
			foreach($album as $data){
				$thumb = MemberImages::model()->findByAttributes(array('album_id'=>$data->id, 'member_id'=>$_POST['memberid']));
				if ($thumb !== null)
				{
					$thumbAlbum[$data->id] = $thumb->image;
				}
			}
			echo $this->renderPartial('viewListAlbumPhoto', array('thumbAlbum'=>$thumbAlbum, 'album'=>$album, 'thumb_album_default'=>$thumb_album_default, 'thumb_album_banner'=>$thumb_album_banner, 'thumb_album_avatar'=>$thumb_album_avatar), false, true);
	}

	public function actionViewListAlbumAvatar()
	{
 
			$thumbAlbum = array();
			$album = MemberAlbums::model()->findAllByAttributes(array('member_id'=>$_POST['memberid']));
			$thumb_album_default = MemberImages::model()->findByAttributes(array('album_id'=>0, 'member_id'=>$_POST['memberid']));
			$thumb_album_banner = MemberCovers::model()->findByAttributes(array('member_id'=>$_POST['memberid'], 'type'=>'C', 'status'=>'V'));
			$thumb_album_avatar = MemberCovers::model()->findByAttributes(array('member_id'=>$_POST['memberid'], 'type'=>'A', 'status'=>'V'));
			foreach($album as $data){
				$thumb = MemberImages::model()->findByAttributes(array('album_id'=>$data->id, 'member_id'=>$_POST['memberid']));
				if ($thumb !== null)
				{
					$thumbAlbum[$data->id] = $thumb->image;
				}
			}
			echo $this->renderPartial('viewListAlbumAvatar', array('thumbAlbum'=>$thumbAlbum, 'album'=>$album, 'thumb_album_default'=>$thumb_album_default, 'thumb_album_banner'=>$thumb_album_banner, 'thumb_album_avatar'=>$thumb_album_avatar), false, true);
	}

	public function actionGetAlbumAvatar()
	{
			$thumbAlbum = array();
			$album = MemberAlbums::model()->findAllByAttributes(array('member_id'=>$_GET['memberid']));
			$thumb_album_default = MemberImages::model()->findByAttributes(array('album_id'=>0, 'member_id'=>$_GET['memberid']));
			$thumb_album_banner = MemberCovers::model()->findByAttributes(array('member_id'=>$_GET['memberid'], 'type'=>'C', 'status'=>'V'));
			$thumb_album_avatar = MemberCovers::model()->findByAttributes(array('member_id'=>$_GET['memberid'], 'type'=>'A', 'status'=>'V'));
			foreach($album as $data){
				$thumb = MemberImages::model()->findByAttributes(array('album_id'=>$data->id, 'member_id'=>$_GET['memberid']));
				if ($thumb !== null)
				{
					$thumbAlbum[$data->id] = $thumb->image;
				}
			}
			echo $this->renderPartial('getAlbumAvatar', array('thumbAlbum'=>$thumbAlbum, 'album'=>$album, 'thumb_album_default'=>$thumb_album_default, 'thumb_album_banner'=>$thumb_album_banner, 'thumb_album_avatar'=>$thumb_album_avatar), false, true);
	}

	public function actionViewListPhotoAvatar()
	{
			switch ($_POST['type']) {
				case 'D':
					$model = MemberImages::model()->findAllByAttributes(array('album_id'=>0, 'member_id'=>$_POST['memberid']));
					break;
				
				case 'C':
					$model = MemberCovers::model()->findAllByAttributes(array('member_id'=>$_POST['memberid'], 'type'=>$_POST['type']));
					break;

				case 'A':
					$model = MemberCovers::model()->findAllByAttributes(array('member_id'=>$_POST['memberid'], 'type'=>$_POST['type']));
					break;

				case 'M':
					$model = MemberImages::model()->findAllByAttributes(array('album_id'=>$_POST['albumid'], 'member_id'=>$_POST['memberid']));
					break;
			}
			echo $this->renderPartial('viewListPhotoAvatar', array('model'=>$model, 'type'=>$_POST['type']), false, true);
	}

	public function actionUploadImageAvatar()
	{
		Yii::import("ext.EAjaxUpload.qqFileUploader");
        $folder= 'images/Avatar/originimage/';// folder for uploaded files
        $allowedExtensions = array("jpg","jpeg","png", "gif", "JPG", "JPEG", "PNG", "GIF");//array("jpg","jpeg","gif","exe","mov" and etc...
        $sizeLimit = 4 * 1024 * 1024;// maximum file size in bytes
        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
        $result = $uploader->handleUpload($folder);
        $return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
        $fileSize=filesize($folder.$result['filename']);//GETTING FILE SIZE
        $fileName=$result['filename'];//GETTING FILE NAME
        echo $return; 
	}

	public function actionCropAvatar()
	{
		if (Yii::app()->request->isAjaxRequest)
		{
			if ($_POST['typeGet'] == 1)
			{
				$src = 'images/Avatar/originimage/'.$_POST['namephoto'];
				$dest = 'images/Avatar/originimage/'.$_POST['namephoto'];
				$dest_173_150 = 'images/Avatar/173_150/'.$_POST['namephoto'];
				copy($src, $dest);
				// resize photo width = 173 and height = 150
				$this->resizePhoto($dest, $dest_173_150);
			}else
			{
				switch ($_POST['typecover']) {
					case 'D':
						$src = 'images/Member/originimage/'.$_POST['namephoto'];
						$dest = 'images/Avatar/originimage/'.$_POST['namephoto'];
						$dest_173_150 = 'images/Avatar/173_150/'.$_POST['namephoto'];
						copy($src, $dest);
						// resize photo to size width = 998 and height = auto;
						$this->resizePhoto($src, $dest_173_150);
						break;
					
					case 'C':
						$src = 'images/Banner/originimage/'.$_POST['namephoto'];
						$dest = 'images/Avatar/originimage/'.$_POST['namephoto'];
						$dest_173_150 = 'images/Avatar/173_150/'.$_POST['namephoto'];
						copy($src, $dest);
						// resize photo to size width = 998 and height = auto;
						$this->resizePhoto($src, $dest_173_150);
						break;

					case 'A':
						$src_resize = 'images/Avatar/resize/'.$_POST['namephoto'];
						$src_crop = 'images/Avatar/crop/'.$_POST['namephoto'];
						$dest = 'images/Avatar/originimage/'.$_POST['namephoto'];
						if (file_exists($src_resize))
						{
							unlink($src_resize);
							unlink($src_crop);
						}
						break;

					case 'M':
						$src = 'images/Member/originimage/'.$_POST['namephoto'];
						$dest = 'images/Avatar/originimage/'.$_POST['namephoto'];
						$dest_173_150 = 'images/Avatar/173_150/'.$_POST['namephoto'];
						copy($src, $dest);
						// resize photo to size width = 998 and height = auto;
						$this->resizePhoto($src, $dest_173_150);
						break;
				}
			}
			Yii::import("ext.EPhpThumb.EPhpThumb");
			$crop=new EPhpThumb();
			$crop->init();
			$y = $_POST['y'];
		    $h = $_POST['h'];
		    $x = $_POST['x'];
		    $w = $_POST['w'];
		    $dest_crop = 'images/Avatar/crop/'.$_POST['namephoto'];
			$dest_resize = 'images/Avatar/resize/'.$_POST['namephoto'];
		   	if ($h > 110)
		    {
				$crop->create($dest)->crop($x, $y, $w, $h)->save($dest_crop);
				$crop->create($dest_crop)->resize(110,110)->save($dest_resize);
			}else
			{
				$crop->create($dest)->crop($x, $y, $w, $h)->save($dest_crop);
				$crop->create($dest)->crop($x, $y, $w, $h)->save($dest_resize);
			}
			// database
			Members::model()->updateByPk($_POST['memberid'], array('image'=>$_POST['namephoto']));
			$avatarPast = MemberCovers::model()->findByAttributes(array('member_id'=>$_POST['memberid'], 'status'=>'V', 'type'=>'A'));
			if ( $_POST['typecover'] != 'A')
			{
				$model = new MemberCovers;
				$model->member_id = $_POST['memberid'];
				$model->image = $_POST['namephoto'];
				$model->originimage = $_POST['namephoto'];
				$model->status = 'V';
				$model->type = 'A';
				$model->posteddate = date('Y-m-d H:i:s');
				if ($model->save(false))
				{
					if ($avatarPast !== null)
						MemberCovers::model()->updateByPk($avatarPast->id, array('status'=>'H'));
				}
			}else
			{
				$model = MemberCovers::model()->findByAttributes(array('member_id'=>$_POST['memberid'], 'originimage'=>$_POST['namephoto'], 'type'=>'A'));
				$model->attributes = array('status'=>'V');
				if ($model->save(false))
				{
					if ($avatarPast !== null && $model->id != $avatarPast->id)
						MemberCovers::model()->updateByPk($avatarPast->id, array('status'=>'H'));
				}
			}
			echo $model->image.'?'.rand(0, 1000);
		}
	}

	public function resizePhoto($src, $dest)
	{
		list($width, $height, $type, $attr) = getimagesize($src);
		if ($width >= $height)
		{
			$height_173_150 = 150;
			$tile_height = 150 / $height;
			$width_173_150 = $width * $tile_height;
		}else
		{
			$width_173_150 = 173;
			$tile_width = 173 / $width;
			$height_173_150 = $height * $tile_width;
		}
		Yii::import("ext.EPhpThumb.EPhpThumb");
			$resize=new EPhpThumb();
			$resize->init();
			$resize->create($src)->resize($width_173_150, $height_173_150)->save($dest);
	}

	public function resizePhotoBanner($src, $dest)
	{
		list($width, $height, $type, $attr) = getimagesize($src);
		if ($width > 1040)
		{
			Yii::import("ext.EPhpThumb.EPhpThumb");
			$resize=new EPhpThumb();
			$resize->init();
			$tile998 = 1040 / $width;
			$height998 = $height * $tile998;
			$resize->create($src)->resize(1040, $height998)->save($dest);
		}else
		{
			copy($src, $dest);
		}
	}

}
 ?>
